package com.jvm.threadgroup;

// 为线程组指定父线程组

import java.util.concurrent.TimeUnit;

/**
 * 创建线程组的时候，可以给其指定一个父线程组，也可以不指定，
 * 如果不指定父线程组，则父线程组为当前线程的线程组
 */
public class Demo2 {

    public static class R1 implements Runnable {

        @Override
        public void run() {
            Thread thread = Thread.currentThread();

            System.out.println("所属线程组：" + thread.getThreadGroup().getName()
                + "，线程名称：" + thread.getName()
            );

            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {

        /**
         *
         * threadGroup1 未指定父线程组，系统获取了主线程的线程组作为 threadGroup1 的父线程组，输出结果中是：main
            threadGroup1 为 threadGroup2 的父线程组
            threadGroup1 活动线程数为 4，包含了 threadGroup1 线程组中的 t1、t2，以及子线程组 threadGroup2 中的 t3、t4
            线程组的 list()方法，将线程组中的所有子孙节点信息输出到控制台，用于调试使用
         *
         */
        ThreadGroup threadGroup1 = new ThreadGroup("thread-group-1");
        Thread t1 = new Thread(threadGroup1, new R1(), "t1");
        Thread t2 = new Thread(threadGroup1, new R1(), "t2");

        t1.start();
        t2.start();
        TimeUnit.SECONDS.sleep(1);

        System.out.println("threadGroup1活动线程数：" + threadGroup1.activeCount());
        System.out.println("threadGroup1活动线程组：" + threadGroup1.activeGroupCount());
        System.out.println("threadGroup1线程组名称：" + threadGroup1.getName());
        System.out.println("threadGroup1父线程组名称：" + threadGroup1.getParent());
        System.out.println("----------------------------------");

        ThreadGroup threadGroup2 = new ThreadGroup(threadGroup1, "thread-group-2");
        Thread t3 = new Thread(threadGroup2, new R1(), "t3");
        Thread t4 = new Thread(threadGroup2, new R1(), "t4");

        t3.start();
        t4.start();
        TimeUnit.SECONDS.sleep(1);

        System.out.println("threadGroup2活动线程数：" + threadGroup2.activeCount());
        System.out.println("threadGroup2活动线程组：" + threadGroup2.activeGroupCount());
        System.out.println("threadGroup2线程组名称：" + threadGroup2.getName());
        System.out.println("threadGroup2父线程组名称：" + threadGroup2.getParent());
        System.out.println("----------------------------------");

        System.out.println("threadGroup1活动线程数：" + threadGroup1.activeCount());
        System.out.println("threadGroup1活动线程组：" + threadGroup1.activeGroupCount());
        System.out.println("----------------------------------");

        threadGroup1.list();


    }

}
